using System;
using System.Collections.Generic;
using System.Linq;
using VoiceAd.Models;

namespace VoiceAd.DAL
{
	public class AreaRepository : IAreaRepository, IDisposable
	{
		private dbVoiceAdDataContext context = new dbVoiceAdDataContext();

		public AreaRepository(dbVoiceAdDataContext db)
		{
			this.context = db;
		}

		public IEnumerable<tblArea>GetListAreas()
		{
			return context.tblAreas.ToList();
		}

		public tblArea GetAreaById(int id)
		{
			return context.tblAreas.Where(x => x.Id == id).FirstOrDefault();
		}

		public void InsArea(tblArea tblarea)
		{
			context.tblAreas.InsertOnSubmit(tblarea);
			context.SubmitChanges();
		}

		public void UpdArea(tblArea tblarea)
		{
			tblArea obj =  context.tblAreas.Where(x => x.Id == tblarea.Id).First();
			obj.AreaName = tblarea.AreaName;
			context.SubmitChanges();
		}

		public void DelArea(int id)
		{
			tblArea obj =  context.tblAreas.Single(x => x.Id == id);
			context.tblAreas.DeleteOnSubmit(obj);
			context.SubmitChanges();
		}

		private bool disposed = false;

		protected virtual void Dispose(bool disposing)
		{
			if (!this.disposed)
			{
				if (disposing)
				{
					context.Dispose();
				}
			}
			this.disposed = true;
		}

		public void Dispose()
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}
	}
}
